<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			*{
				margin: 0;
				padding: 0;
			}
			#box{
				width: 500px;
				height: 400px;
				border: 10px solid gray;
				margin: 20px auto 0;
				position: relative;
			}
			#BOX{
				width: 50px;
				height: 50px;
				background: green;
				border-radius: 50%;
				position: absolute;
			}
		</style>
	</head>
	<body>
		<div id="box"><div id="BOX"></div></div>
	</body>
	<script type="text/javascript">
		var box = document.getElementById("box");
		var BOX = document.getElementById("BOX");
		var insetX = 0;
		var insetY = 0;
		var originleft = BOX.offsetLeft; 
		var origintop = BOX.offsetTop; 
//		alert(originleft);
		var maxWidth = box.clientWidth-BOX.offsetWidth;
		var maxHeight = box.clientHeight-BOX.offsetHeight;
		var addX = 5;
		var addY = 10;
		setInterval(function(){
			insetX += addX;
			insetY += addY;
			if(insetX >= maxWidth){
				addX = -addX;
			}
			if(insetX <= 0){
				addX = -addX;
			}
			if(insetY/2 >= maxHeight){
				addY = -addY;
			}
			if(insetY/2 <= 0){
				addY = -addY;
			}
			BOX.style.top = origintop + insetY/2 + "px";
			BOX.style.left = originleft + insetX + "px";
		},30)
	</script>
</html>